import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({ showSpinner: false })
Vue.use(VueRouter)

import VueClipboard from 'vue-clipboard2'
// import { component } from 'vue/types/umd'

Vue.use(VueClipboard)

const routes = [{
	path: '/',
	name: 'home',
	component: HomeView,
	meta: {
		keep: true,
	}
},
{
	path: "/category",
	// 只要需要权限的页面，都配置一个meta元信息
	meta: {
		keep: true,
		// requireAuth: true
	},
	name: "category",
	component: () => import("../views/CategoryView.vue")
},
{
	path: "/ball",
	meta: {
		requireAuth: true
	},
	name: "ball",
	component: () => import("../views/BallView.vue")
},

{
	// 定义一个元信息，标识当前页面不需要路由
	meta: { noFoot: true },
	path: "/cart",
	name: "cart",
	component: () => import("../views/CartView.vue")
},
{
	meta: { noFoot: true },
	// 带参数的路由 /:
	path: "/product/:id",
	name: "product",
	component: () => import("../views/ProductView.vue")
},
{
	meta: { noFoot: true },
	path: "/search/list",
	name: "slist",
	component: () => import("../views/search/SearchList.vue")
},
{
	meta: { noFoot: true },
	path: "/ac",
	name: "ac",
	component: () => import("../views/WaterFall.vue")
},
{
	path: "/user",
	name: "user",
	component: () => import("../views/user/UserView.vue")
},
{
	path: "/login",
	name: "login",
	beforeEnter(to, from, next) {
		console.log("登录进入前");
		next(true);
	},
	component: () => import("../views/user/LoginView.vue")
},
{
	meta: { noFoot: true },
	path: "/reg",
	name: "reg",
	component: () => import("../views/user/RegisterView.vue")
},
{
	meta: { noFoot: true },
	path: "/abcc",
	name: "abcc",
	component: () => import("../views/abcView.vue")
},
{
	path: '/about',
	name: 'about',
	// route level code-splitting
	// this generates a separate chunk (about.[hash].js) for this route
	// which is lazy-loaded when the route is visited.
	component: () => import( /* webpackChunkName: "about" */ '../views/AboutView.vue')
},
{
	path:'/map',
	name:'map',
	component:()=>import('../views/MapView.vue')
},
{
	path:'/bai',
	name:'bai',
	component:()=>import('../views/BaiduView.vue')
}
]

const router = new VueRouter({
	routes
})
router.beforeEach((to, from, next) => {
	// 如果要去的也有meta.requireAuth 判断是否登录
	NProgress.start();
	NProgress.done();
	if (to.meta.requireAuth) {
		if (localStorage.getItem("token")) {
			next(true);
		} else {
			alert("需要权限，请先登录");
			next("/login?redirect=" + to.path)
		}
	} else {
		next(true);
	}
})
// 全局导航守卫函数 beforeEach每个路由进入都会先调用这个函数
// afterEach 每个路由离开前都会调用这个函数
// 在afterEach里面 先判断元信息meta是不是requireAuth
// 如果是requireAuth 判断是否是已经登录，
// 如果已经登录 next放行 
// 如果没有登录 next(/login)

export default router
